package groovy.jmx.builder

import groovy.jmx.builder.node.ParamNodeData

class JmxOpParamFactory extends AbstractFactory {
    ParamNodeData data

    public Object newInstance(FactoryBuilderSupport builder, Object nodeName, Object nodeArgs, Map nodeAttribs) {
        // look for at most the name attribute
        if (!nodeAttribs.containsKey("type")) {
            throw new JmxBuilderException("You must provide at least the type of the parameter.")
        }
        data = new ParamNodeData(
                type: nodeAttribs.remove('type'),
                name: nodeAttribs.remove('name'),
                description: nodeAttribs.remove('description') ?: nodeAttribs.remove('desc')
        )
        return data
    }

    public boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map nodeAttribs) {
        return true;
    }

    public void setParent(FactoryBuilderSupport builder, Object parentNode, Object childNode) {
        parentNode.parameters.add(childNode)
    }

    public boolean isLeaf() {
        return true
    }
}